<?php

class Form
{
    static public function input($field, $validators = '', $defaultValue = '', $method = 'POST')
    {
        if (!isset($GLOBALS['formErrors'])) {
            $GLOBALS['formErrors'] = array();
        }

        $errorVariable = 'err_' . $field;
        $GLOBALS[$errorVariable] = '';

        $validate = false;
        $value = '';

        if ($method === $_SERVER['REQUEST_METHOD']) {
            $validate = true;
            if ('POST' === $method) {
                if (isset($_POST[$field]) && $_POST[$field] !== '') {
                    $value = $_POST[$field];
                } else {
                    $value = $defaultValue;
                }
            } else {
                if (isset($_GET[$field]) && $_GET[$field] !== '') {
                    $value = $_GET[$field];
                } else {
                    $value = $defaultValue;
                }
            }
        }

        if ($validators !== '' && $validate) {
            $validators = explode(',', $validators);
            $value = Str::s2n($value);
            foreach ($validators as $validator)
            {
                if ('ne' === $validator && '' == $value) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_notempty');
                } elseif ('@' === $validator && !preg_match('#^([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4}))?$#si', $value)) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_email');
                } elseif ('a' === $validator && !preg_match('#^[a-zA-Z]*$#', $value)) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_a');
                } elseif ('0' === $validator && !preg_match('#^\d*$#', $value)) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_0');
                } elseif ('a0' === $validator && !preg_match('#^[a-zA-Z0-9]*$#', $value)) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_a0');
                } elseif ('a1' === $validator && !preg_match('#^[a-zA-Z0-9_-]*$#', $value)) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_a1');
                } elseif ('date' === $validator && !preg_match('#^[0-9_/\\-]*$#', $value)) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_a1');
                } elseif ('>>' === substr($validator, 0, 2) && $value <= substr($validator, 2)) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_greater', substr($validator, 2));
                } elseif ('<<' === substr($validator, 0, 2) && $value >= substr($validator, 2)) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_less', substr($validator, 2));
                } elseif ('>' === substr($validator, 0, 1) && strlen($value) <= substr($validator, 1)) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_longer', substr($validator, 1));
                } elseif ('<' === substr($validator, 0, 1) && strlen($value) >= substr($validator, 1)) {
                    $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_shorter', substr($validator, 1));
                } elseif ('L' === substr($validator, 0, 1)) {
                    $nums = explode(':', substr($validator, 1));
                    $len = strlen($value);
                    if ($len < $nums[0] || $len > $nums[1]) {
                        $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_length_range', $nums[0], $nums[1]);
                    }
                } elseif ('V' === substr($validator, 0, 1)) {
                    $nums = explode(':', substr($validator, 1));
                    $len = strlen($value);
                    if ($len < $nums[0] || $len > $nums[1]) {
                        $GLOBALS[$errorVariable] .= ($GLOBALS[$errorVariable] != '' ? '<br />' : '') . fl('err_range', $nums[0], $nums[1]);
                    }
                }

                if (!empty($GLOBALS[$errorVariable])) {
                    $GLOBALS['formErrors'][] = $field;
                }
            }
        }

        return $value;
    }

    /**
     * Check if the form input values are valid
     * @return boolean
     */
    static public function isValid()
    {
        return !isset($GLOBALS['formErrors'][0]);
    }

    static public function err($field, $prefix = '', $suffix = '')
    {
        return isset($GLOBALS['err_' . $field]) ? $prefix . $GLOBALS['err_' . $field] . $suffix : null;
    }
}
